<template>
  <div class="main-wrap">
    <div class="header">
      <div class="left">
        <div class="curr">
          <img src="https://lf-cdn-tos.bytescm.com/obj/static/xitu_extension/static/github.46c47564.png" alt="gold"
            class="icon source-icon" data-v-96420278="" /><!----><!---->
        </div>
        <div class="icon">
          <span class="title">GitHub</span>
          <span class="el-icon-caret-bottom"></span>
        </div>
      </div>
      <div class="right">
        <a-select :default-value="selectOption.option1.defaultValue" style="width: 66px" size="small" @change="change1">
          <span slot="suffixIcon" class="el-icon-caret-bottom" />
          <a-select-option :value="item.value" v-for="item in selectOption.option1.optionList">
            {{ item.name }}
          </a-select-option>
        </a-select>

        <a-select :default-value="selectOption.option2.defaultValue" style="width: 66px; margin-left: 16px" size="small"
          @change="change2">
          <span slot="suffixIcon" class="el-icon-caret-bottom" />
          <a-select-option :value="item.value" v-for="item in selectOption.option2.optionList">
            {{ item.name }}
          </a-select-option>
        </a-select>

        <a-select :default-value="selectOption.option3.defaultValue" style="width: 82px; margin-left: 16px" size="small"
          @change="change3">
          <span slot="suffixIcon" class="el-icon-caret-bottom" />
          <a-select-option :value="item.value" v-for="item in selectOption.option3.optionList">
            {{ item.name }}
          </a-select-option>
        </a-select>
      </div>
    </div>
    <div class="main">
      <div class="nodata" v-if="tableData.length === 0">
        —— 已显示全部内容 ——
      </div>
      <div class="tabledata" v-if="tableData.length != 0">
        <div class="itemwrap" v-for="item in tableData" :key="item.id">
          <a class="title">
            <span class="author">{{ item.username }}</span>
            <span class="divider"> / </span>
            <span class="name">{{ item.reponame }}</span>
          </a>
          <div class="des">
            {{ item.description }}
          </div>
          <div class="repometa">
            <span class="el-icon-star-on">{{ formatStr(item.starCount) }}</span>
            <div class="fork-wrap">
              <i class="fork">
                <svg t="1681282888943" class="icon" viewBox="0 0 1024 1024" version="1.1"
                  xmlns="http://www.w3.org/2000/svg" p-id="2762" width="200" height="200">
                  <path
                    d="M832 160 736 160 736 64 608 64 608 160 512 160 512 288 608 288 608 384 736 384 736 288 832 288Z"
                    p-id="2763" fill="#86909C"></path>
                  <path
                    d="M608 480c0 77.2-9 84.6-28.8 100.6-14.8 12-44.4 14.2-78.8 16.6-19 1.4-40.8 3-62.8 6.6-18.8 3-36.8 9.4-53.6 17.6L384 302.8c38.2-22.2 64-63.4 64-110.8 0-70.6-57.4-128-128-128S192 121.4 192 192c0 47.4 25.8 88.6 64 110.8l0 418.4c-38.2 22.2-64 63.4-64 110.8 0 70.6 57.4 128 128 128s128-57.4 128-128c0-31.4-11.4-60.2-30-82.4 13.4-9.6 27.8-17.4 40.4-19.4 16.2-2.6 33.2-3.8 51.2-5.2 48.8-3.4 104.2-7.4 150.4-45 61.8-50.2 75-104.2 76-189.8l0 0L736 480 608 480zM320 128c35.2 0 64 28.8 64 64s-28.8 64-64 64-64-28.8-64-64S284.8 128 320 128zM320 896c-35.2 0-64-28.8-64-64s28.8-64 64-64 64 28.8 64 64S355.2 896 320 896z"
                    p-id="2764" fill="#86909C"></path>
                </svg>
              </i>
              <i>{{ item.forkCount }}</i>
            </div>
            <div class="lang">
              <i :style="{ background: item.langColor }"></i>
              <span>{{ item.lang }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { langList } from "./lang.js"
export default {
  data() {
    return {
      options: ["trending", "day", "java"],
      selectOption: {
        option1: {
          defaultValue: "trending",
          optionList: [
            { id: "1", name: "热门", value: "trending" },
            { id: "2", name: "新生", value: "upcome" },
          ]
        },
        option2: {
          defaultValue: "day",
          optionList: [
            { id: "1", name: "今日", value: "day" },
            { id: "2", name: "本周", value: "week" },
            { id: "3", name: "本月", value: "month" },
          ]
        },
        option3: {
          defaultValue: "java",
          optionList: [
          ]
        },
      },
      tableData: [],
    };
  },

  created() {
    // 首先处理第三个选项，第三个选项数据太多只能靠别的方式进行修改
    for (let index = 0; index < langList.length; index++) {
      this.selectOption.option3.optionList.push({ id: `${index + 1}`, name: langList[index], value: langList[index].toLowerCase() })
    }
    this.getMainArticles()
  },
  methods: {
    async getMainArticles() {
      this.tableData = []
      await fetch("https://e.juejin.cn/resources/github", {
        headers: {
          "content-type": "application/json",
          "sec-ch-ua": "\"Chromium\";v=\"110\", \"Not A(Brand\";v=\"24\", \"Google Chrome\";v=\"110\"",
          "sec-ch-ua-mobile": "?0",
          "sec-ch-ua-platform": "\"Windows\"",
          "x-extension-version": "web"
        },
        referrer: "https://e.juejin.cn/",
        "referrerPolicy": "strict-origin-when-cross-origin",
        body: `{"category":"${this.options[0]}","period":"${this.options[1]}","lang":"${this.options[2]}","offset":0,"limit":30}`,
        method: "POST",
      }).then((res) => {
        res.json().then((data) => {
          for (let i = 0; i < data.data.length; i++) {
            this.tableData.push(data.data[i]);
          }
        });
      });
    },

    change1(v) {
      this.options[0] = v
      this.getMainArticles()
    },
    change2(v) {
      this.options[1] = v
      this.getMainArticles()
    },
    change3(v) {
      this.options[2] = v
      this.getMainArticles()
    },
    formatStr(n) {
      if (Number(n) > 10000) {
        return Number(n / 10000).toFixed(0) + "W+";
      } else if (Number(n) > 1000) {
        return Number(n / 1000).toFixed(0) + "k+";
      } else {
        return n;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.main-wrap {
  min-height: 44px;
  border-radius: 2px 2px 0 0;
  height: 100%;
  display: flex;
  flex-flow: column;

  .header {
    padding: 0 12px;
    display: flex;
    height: 44px;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid rgb(201, 205, 212);
    background: #fff;

    .left {
      display: flex;
      height: 44px;
      align-items: center;

      .curr {
        width: 28px;
        height: 28px;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-right: 16px;

        img {
          width: 100%;
        }
      }

      .icon {
        color: #1d2129;
        display: flex;
        align-items: center;
        cursor: pointer;

        .title {
          font-size: 16px;
          margin-right: 10px;
        }

        .el-icon-caret-bottom {
          width: 12px;
          height: 18px;
          color: #c9cdd4;
          transform: translateY(1px);
        }
      }
    }

    .right {
      height: 44px;
      display: flex;
      align-items: center;
      font-size: 14px;
      color: #4e5969;
      cursor: pointer;

      .hottest {
        transition: all 0.15s linear;
        display: flex;
        align-items: center;
        color: #4e5969;

        .el-icon-refresh {
          width: 18px;
          height: 18px;
          margin-right: 3px;
          font-size: 15px;
          font-weight: bold;
        }
      }
    }
  }

  .main {
    overflow: auto;

    .nodata {
      height: 72px;
      display: flex;
      justify-content: center;
      align-items: center;
      color: #86909c;
      background: #fff;
    }

    .tabledata {
      background: #fff;
      display: flex;
      justify-content: space-between;
      align-items: start;
      flex-wrap: wrap;
      padding: 8px;
      box-sizing: border-box;
      .itemwrap {
        min-width: 300px;
        flex-basis: 50%;
        flex-grow: 1;
        padding: 8px 12px;
        background: rgb(249, 250, 250);
        width: calc(700 / 1920 * 100vw);
        height: 110px;
        margin-bottom: 8px;

        .title {
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          font-size: 16px;
          font-weight: 600;
          color: #1e80ff;
          line-height: 22px;
          margin-bottom: 10px;

          &:hover {
            text-decoration: underline;
          }
        }

        .des {
          font-size: 13px;
          line-height: 22px;
          color: #4e5969;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          margin-top: 4px;
          height: 44px;
        }

        .repometa {
          display: flex;
          margin-top: 8px;
          color: #86909c;
          font-size: 14px;
          align-items: center;

          span {
            display: inline-block;
            font-size: 12px;
            margin-right: 25px;
          }

          .fork-wrap {
            display: flex;
            align-items: center;
            margin-right: 25px;

            .fork {
              width: 12px;
              height: 12px;
              display: flex;
              justify-content: center;
              align-items: center;
            }

            i {
              font-style: normal;
              font-size: 12px;
            }
          }

          .lang {
            display: flex;
            align-items: center;

            i {
              display: flex;
              width: 12px;
              height: 12px;
              border-radius: 50%;
              background-color: rgb(241, 224, 90);
              margin-right: 5px;
            }
          }
        }
        &:hover{
          background-color:darkgray;
        }
      }
    }
  }
}
</style>
